<?php
class UsersController extends AppController {

	var $name = 'Users';
	var $components = array('Auth');

	function beforeFilter()
	{
            $this->Auth->loginError = 'Le login et/ou le mot de passe est incorrect';
            $this->Auth->allow('add');
            $this->Auth->fields = array('username' => 'email','password' => 'password');
	}
	

	function index() {
            $this->User->recursive = 0;
            $this->set('users', $this->paginate());
	}
	
	function index_etudiant() {
		
		$list_student = $this->User->findAllByParent_id($this->Session->read('Auth.User.id'));
		$this->set('students', $list_student);
	}
	
	function chargeplacement() {
		
		//$chargerde = $this->User->findById($this->Session->read('Auth.User.id'));
		$this->set('charger', $this->User->findById($this->Session->read('Auth.User.parent_id')));
	}

        function index_list_members($idTypeMember)
        {
            
            $list_members = $this->User->findAllByPrivilege_id($idTypeMember);
            $this->set('users',$list_members);
        }



	function view($id = null) {
            if (!$id) {
                    $this->Session->setFlash(__('Invalid user', true));
                    $this->redirect(array('action' => 'index'));
            }
            $this->set('user', $this->User->read(null, $id));
				
	}

	function add() {
            $this->set('title_for_layout', 'Nouvel utilisateur');
            if (!empty($this->data)) {
                    $this->User->create();
                    if ($this->User->save($this->data)) {
                            $this->Session->setFlash(__('The user has been saved', true));
                            $this->redirect(array('action' => 'index'));
                    } else {
                            $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
                    }
            }
            $privileges = $this->User->Privilege->find('list', array('fields' => array('label')));
            $this->set(compact('privileges'));

	}

	function edit($id = null) {
            if (!$id && empty($this->data)) {
                    $this->Session->setFlash(__('Invalid user', true));
                    $this->redirect(array('action' => 'index'));
            }
            if (!empty($this->data)) {
                    if ($this->User->save($this->data)) {
                            $this->Session->setFlash(__('The user has been saved', true));
                            $this->redirect(array('action' => 'index'));
                    } else {
                            $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
                    }
            }
            if (empty($this->data)) {
                    $this->data = $this->User->read(null, $id);
            }
            $privileges = $this->User->Privilege->find('list', array('fields' => array('label')));
            $this->set(compact('privileges'));
	}

	function delete($id = null) {
            if (!$id) {
                    $this->Session->setFlash(__('Invalid id for user', true));
                    $this->redirect(array('action'=>'index'));
            }
            if ($this->User->delete($id)) {
                    $this->Session->setFlash(__('User deleted', true));
                    $this->redirect(array('action'=>'index'));
            }
            $this->Session->setFlash(__('User was not deleted', true));
            $this->redirect(array('action' => 'index'));
	}
	function admin_index() {
            $this->User->recursive = 0;
            $this->set('users', $this->paginate());
	}

	function admin_view($id = null) {
            if (!$id) {
                    $this->Session->setFlash(__('Invalid user', true));
                    $this->redirect(array('action' => 'index'));
            }
            $this->set('user', $this->User->read(null, $id));
	}

	function admin_add() {
            if (!empty($this->data)) {
                    $this->User->create();
                    if ($this->User->save($this->data)) {
                            $this->Session->setFlash(__('The user has been saved', true));
                            $this->redirect(array('action' => 'index'));
                    } else {
                            $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
                    }
            }
            $privileges = $this->User->Privilege->find('list');
            $this->set(compact('privileges'));
	}

	function admin_edit($id = null) {
            if (!$id && empty($this->data)) {
                    $this->Session->setFlash(__('Invalid user', true));
                    $this->redirect(array('action' => 'index'));
            }
            if (!empty($this->data)) {
                    if ($this->User->save($this->data)) {
                            $this->Session->setFlash(__('The user has been saved', true));
                            $this->redirect(array('action' => 'index'));
                    } else {
                            $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
                    }
            }
            if (empty($this->data)) {
                    $this->data = $this->User->read(null, $id);
            }
            $privileges = $this->User->Privilege->find('list');
            $this->set(compact('privileges'));
	}

	function admin_delete($id = null) {
            if (!$id) {
                    $this->Session->setFlash(__('Invalid id for user', true));
                    $this->redirect(array('action'=>'index'));
            }
            if ($this->User->delete($id)) {
                    $this->Session->setFlash(__('User deleted', true));
                    $this->redirect(array('action'=>'index'));
            }
            $this->Session->setFlash(__('User was not deleted', true));
            $this->redirect(array('action' => 'index'));
	}
	
	/* Connexion USER */
	
	function login()
	{
            $this->set('title_for_layout', 'Connexion');
                if ($this->Session->read('Auth.User')) {
                    print_r($this->User->findById($this->Session->read('Auth.User')));
                    $this->Session->setFlash('Tu es connecte !');
                    $userCurrent = $this->User->findById($this->Session->read('Auth.User'));
                    if($userCurrent['User']['privilege_id'] == 3) :
                        $this->Session->write('User.privilege', '3');
                    endif;
                    $this->redirect('/', null, false);
		}
        }
	   
	function logout()
	{
            $this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
            $this->Session->setFlash('Aurevoir');
            $this->redirect($this->Auth->logout());
	}


	// recherche
	function searchKeywords()
	{
		// recherche par titre, langue, interets, categories de cv ,competences
				
		
		$this->User->recursive = 1;
		$this->set('users', $this->paginate());
		$keywords = $this->data['searchForm']['keywords'];
		$idCatKeywords = (!empty($this->data['searchForm']['idCatKeywords']) ? $this->data['searchForm']['idCatKeywords'] : '3');
		$this->set('keywords',$keywords);
	       
		if (!empty($this->data)) {     
		  
		  
		  switch ($idCatKeywords)
		  {
			case '1': // titre de CV
				$list_cvs = $this->User->query("SELECT c.id, c.title , u.id, u.firstname, u.lastname,u.privilege_id
								FROM cvs c, users u 
								WHERE c.user_id = u.id
								AND U.privilege_id = 1
								AND title LIKE '%".$keywords."%'");
				break;
			
			case '2': //interets
				$list_cvs = $this->User->query("SELECT c.id, c.title , u.id, u.firstname, u.lastname,u.privilege_id
								FROM cvs c, users u 
								WHERE c.user_id = u.id
								AND U.privilege_id = 1
								AND interests LIKE '%".$keywords."%'");
				break;
			
			case '3': // categories de CV
				$list_cvs = $this->User->query("SELECT c.id, c.title , u.id, u.firstname, u.lastname,u.privilege_id
								FROM cvs c, users u, categories_cvs catcvs, categories cat 
								WHERE c.user_id = u.id
								AND c.id = catcvs.cv_id
								AND catcvs.category_id = cat.id
								AND U.privilege_id = 1
								AND cat.label LIKE '%".$keywords."%'");
				break;
			
			case '4': // langues
				$list_cvs = $this->User->query("SELECT c.id, c.title , u.id, u.firstname, u.lastname,u.privilege_id
								FROM cvs c, users u, languages_cvs langcvs, languages lang 
								WHERE c.user_id = u.id
								AND c.id = langcvs.cv_id
								AND langcvs.language_id = lang.id
								AND U.privilege_id = 1
								AND lang.label LIKE '%".$keywords."%'");
				break;
			
			case '5': //competences
				$list_cvs = $this->User->query("SELECT c.id, c.title , u.id, u.firstname, u.lastname,u.privilege_id
								FROM cvs c, users u, skills_cvs skcvs, skills sk 
								WHERE c.user_id = u.id
								AND c.id = skcvs.cv_id
								AND skcvs.skill_id = sk.id
								AND U.privilege_id = 1
								AND sk.label LIKE '%".$keywords."%'");
				break;
			
			default: // par defaut, il recherche ds la categorie de CV
				break;
		  }
		  
		    	  
		  $this->set(compact('list_cvs'));
		  		  
		}
	}

}
?>